package com.example.voicerecorder;

import java.io.File;

import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {
	MediaRecorder recorder;
	private File mOutputFile;
	Button recordButton;
	Button stopButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initialButton();
		recordButton.setOnClickListener(startListerner);
		stopButton.setOnClickListener(stopListerner);
		recorder = new MediaRecorder();
		mOutputFile = new File("/storage/extSdCard/source.3gp");
		
	}

	private void initialButton() {
		// TODO Auto-generated method stub
		recordButton = (Button) findViewById(R.id.btnRecord);
		stopButton = (Button) findViewById(R.id.butnStop);
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		if(recorder != null){
			recorder.release();
			recorder = null;
		}
	} 
	
	
	private View.OnClickListener startListerner = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
			recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			recorder.setOutputFile(mOutputFile.getAbsolutePath());
			try{
				recorder.prepare();
			}catch(Exception e){
				e.printStackTrace();
			}
		
			recorder.start();
			recordButton.setEnabled(false);
			stopButton.setEnabled(true);
		}
	};
	
	
private View.OnClickListener stopListerner = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
		
			recorder.stop();
			recordButton.setEnabled(true);
			stopButton.setEnabled(false);
			
		}
	};

}
